<?php
$db = new Database();
$fs = new Functions();
$set_date_time = $fs->set_default_timezone_datetime();
$auth = Auth::getAuth('current_user');
$result = array();
//if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST)){
    
    if(isset($_GET['action'])){
        
        // Add New Role in the system   
        if($_GET['action'] == "add_role"){
            
            $roles = new Roles($db,$auth['id']);
            $roles->role_name = $_POST['role_name'];	
            $roles->is_active = "1";
            $roles->save();
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "New role was successfully saved.",
                            "form_name"     =>  "#add_role",
                            "type"          =>  "role");
            
            echo json_encode($return);
            
            $fs->save_audit_logs($db,$auth['id'],"24",TB_ROLE_ACCESS,$set_date_time['date_time'],$roles->id);
        }
        
        // Add New Department Unit
        elseif($_GET['action'] == "add_department"){
            
            $department = new Department_Unit($db,$auth['id']);
            $department->department_name = $_POST['department_name'];
            $department->role_id = $_POST['dep_access_role'];	
            $department->is_active = "1";
            $department->save();
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "New department was successfully saved.",
                            "form_name"     =>  "#add_department_unit",
                            "post"=>$_POST);
            
            echo json_encode($return);
            
            $fs->save_audit_logs($db,$auth['id'],"26",TB_DEPARTMENT,$set_date_time['date_time'],$department->id);
        }
        
        // Add New Brand Category
        elseif($_GET['action'] == "add_brand"){
            
            $brand = new Brand_Category($db,$auth['id']);
            $brand->brand_title = $_POST['brand_title'];
            $brand->brand_category = $_POST['brand_category'];
            $brand->role_id = $_POST['brand_role'];
            $brand->department_id = $_POST['brand_department'];	
            $brand->is_active = "1";
            $brand->save();
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "New brand category was successfully saved.",
                            "form_name"     =>  "#add_brand",
                            "post"          =>  $_POST);
            
            echo json_encode($return);
            
            $fs->save_audit_logs($db,$auth['id'],"28",TB_BRAND_CATEGORY,$set_date_time['date_time'],$brand->id);
        }
        
        // Update Role in the system   
        elseif($_GET['action'] == "update_role"){
            
            $roles = new Roles($db,$_GET['dataID']);
            $roles->role_name = $_POST['role_name'];	
            $roles->update();
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "Role was successfully updated.",
                            "form_name"     =>  "#" . $_GET['action'],
                            "type"          =>  "role");
            
            echo json_encode($return);
            
            $fs->save_audit_logs($db,$auth['id'],"25",TB_ROLE_ACCESS,$set_date_time['date_time'],$roles->id);
        }
        
        // Remove Role in the system   
        elseif($_GET['action'] == "remove_role"){
            
            $roles = new Roles($db,$_GET['dataID']);
            $roles->is_active = "0";	
            $roles->update();
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "Role was successfully removed.",
                            "form_name"     =>  "",
                            "type"          =>  "role");
            
            echo json_encode($return);
            
            $fs->save_audit_logs($db,$auth['id'],"32",TB_ROLE_ACCESS,$set_date_time['date_time'],$roles->id);
        }
        
        // Load Table Role
        elseif($_GET['action'] == "load_table_role"){
            $query = $db->query("SELECT *, id as roleID FROM " . $db->tbl_name(TB_ROLES) . " WHERE is_active={$db->escape(1)}", "array");
            foreach($query as $data){
                
                $result[] = array(
                            "data_reference_id"      =>  $data['roleID'],
                            "roleID"                 =>  $data['roleID'],
                            "role_name"              =>  $data['role_name'],
                            "type"                   =>  "role"
                            );
            }
            $res = array("get_query"    =>  $result);
            
            $string = json_encode($res);
            
            echo Functions::view_json_formatter($string);
            
            //$fs->save_audit_logs($db,$auth['id'],"32",TB_ROLE_ACCESS,$set_date_time['date_time'],$roles->id);
        }
        
        // Load Table Department
        elseif($_GET['action'] == "load_table_department"){
            $query = $db->query("SELECT *,
                            d.id as DepID, d.is_active as dIs_active,
                            r.id as RoleID
                            FROM " . $db->tbl_name(TB_DEPARTMENT) . " d
                            LEFT JOIN " . $db->tbl_name(TB_ROLES) . " r ON d.role_id=r.id
                            WHERE d.is_active={$db->escape(1)}", "array");
            foreach($query as $data){
                
                $result[] = array(
                            "data_reference_id"      =>  $data['DepID'],
                            "roleID"                 =>  $data['RoleID'],
                            "DepID"                  =>  $data['DepID'],
                            "role_name"              =>  $data['role_name'],
                            "department_name"        =>  $data['department_name'],
                            "type"                   =>  "department"
                            );
            }
            $res = array("get_query"    =>  $result);
            
            $string = json_encode($res);
            
            echo Functions::view_json_formatter($string);
        }
        
        // Load list under role
        elseif($_GET['action'] == "select_role"){
            $dataID = $_POST['dataID'];
            $departments = $db->query("SELECT * FROM " . $db->tbl_name(TB_DEPARTMENT) . " WHERE role_id={$db->escape($dataID)} AND is_active={$db->escape(1)}", "array");
            $ret = "";
            $ret .= '<option value="">Choose Department</option>';
            foreach($departments as $department){
                $ret .= '<option value="' . $department["id"] . '">' . $department["department_name"] . '</option>';
            }
            
            echo $ret;
            
        }
        
        // Load list under department
        elseif($_GET['action'] == "select_department"){
            $dataID = $_POST['dataID'];
            $departments = $db->query("SELECT * FROM " . $db->tbl_name(TB_BRAND_CATEGORY) . " WHERE department_id={$db->escape($dataID)} AND is_active={$db->escape(1)}", "array");
            $ret = "";
            $ret .= '<option value="">Choose Brand Category</option>';
            foreach($departments as $department){
                $ret .= '<option value="' . $department["id"] . '">' . $department["brand_category"] . '</option>';
            }
            
            echo $ret;
        }
        
        // Role Access Configuration
        elseif($_GET['action'] == "update_role_access"){
            $dataID = $_GET['id'];
            $arr = array();
            foreach($_POST['access_type'] as $value){
                array_push($arr,$value);
            }
            $decrypted_id = $fs->base_encode_decode("decrypt", $dataID);
            
                $Brochure_Pagination_Update = new Role_Access($db,$decrypted_id);
                $Brochure_Pagination_Update->page_access   =   $_POST['page_access'];	
                $Brochure_Pagination_Update->level_id   =   $_POST['role_access'];	
                $Brochure_Pagination_Update->access_type   =   json_encode($arr,JSON_NUMERIC_CHECK);
                $Brochure_Pagination_Update->modified_by   =   $auth['id'];	
                $Brochure_Pagination_Update->date_modified   =   $set_date_time['date_time'];	
                $Brochure_Pagination_Update->update();
                
                $fs->save_audit_logs($db,$auth['id'],"36",TB_ROLE_ACCESS,$set_date_time['date_time'],$Brochure_Pagination_Update->id);
           
        
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "Role Access was successfully updated.",
                            "form_name"     =>  "#access_role",
                            "type"          =>  "set");
            
            echo json_encode($return);
            
        }
        
        // Update Role Access
        elseif($_GET['action'] == "role_access"){
            ini_set('mssql.charset', 'UTF-8');
            $Brochure_Pagination = new Role_Access($db,$auth['id']);
            
            
            
            
            $Brochure_Validation = $db->query("SELECT * FROM " . $db->tbl_name(TB_ROLE_ACCESS) . " WHERE page_access = {$db->escape($_POST['page_access'])} AND level_id = {$db->escape($_POST['role_access'])}","numrows");
            $Brochure_query = $db->query("SELECT * FROM " . $db->tbl_name(TB_ROLE_ACCESS) . " WHERE page_access = {$db->escape($_POST['page_access'])} AND level_id = {$db->escape($_POST['role_access'])}","row");
            $arr = array();
            foreach($_POST['access_type'] as $value){
                array_push($arr,$value);
            }
            if($Brochure_Validation != "0"){
                $Brochure_Pagination_Update = new Role_Access($db,$Brochure_query['id']);
                $Brochure_Pagination_Update->id == $Brochure_query['id'];
                $Brochure_Pagination_Update->page_access   =   $_POST['page_access'];	
                $Brochure_Pagination_Update->level_id   =   $_POST['role_access'];	
                $Brochure_Pagination_Update->access_type   =   json_encode($arr,JSON_NUMERIC_CHECK);
                $Brochure_Pagination_Update->created_by   =   $auth['id'];
                $Brochure_Pagination_Update->modified_by   =   $auth['id'];	
                $Brochure_Pagination_Update->date_created   =   $set_date_time['date_time'];	
                $Brochure_Pagination_Update->date_modified   =   $set_date_time['date_time'];	
                $Brochure_Pagination_Update->is_active   =   "1";
                $Brochure_Pagination_Update->update();
                
                $fs->save_audit_logs($db,$auth['id'],"36",TB_ROLE_ACCESS,$set_date_time['date_time'],$Brochure_Pagination_Update->id);
            }else{
                $Brochure_Pagination->page_access   =   $_POST['page_access'];	
                $Brochure_Pagination->level_id   =   $_POST['role_access'];	
                $Brochure_Pagination->access_type   =   json_encode($arr,JSON_NUMERIC_CHECK);
                $Brochure_Pagination->created_by   =   $auth['id'];
                $Brochure_Pagination->modified_by   =   $auth['id'];	
                $Brochure_Pagination->date_created   =   $set_date_time['date_time'];	
                $Brochure_Pagination->date_modified   =   $set_date_time['date_time'];	
                $Brochure_Pagination->is_active   =   "1";
                $Brochure_Pagination->save();
                
                $fs->save_audit_logs($db,$auth['id'],"35",TB_ROLE_ACCESS,$set_date_time['date_time'],$Brochure_Pagination_Update->id);
            }
            
        
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "Role Access was successfully set.",
                            "form_name"     =>  "#access_role",
                            "type"          =>  "set");
            
            echo json_encode($return);
            
        }
        
        else{
            $this->setLayout();
        }
    }else if($_POST['action']){
    
        if($_POST['action'] == "remove"){
            $dataID = $_POST['dataID'];
            $decrypted_id = $fs->base_encode_decode("decrypt", $dataID);
            
            $db->delete($db->tbl_name(TB_ROLE_ACCESS),array("id"    => $decrypted_id));
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "Role Access was successfully remove.",
                            "form_name"     =>  "#access_role",
                            "type"          =>  "set");
            
            echo json_encode($return);
            
            $fs->save_audit_logs($db,$auth['id'],"37",TB_ROLE_ACCESS,$set_date_time['date_time'],$decrypted_id);
        }
        else{
            $this->setLayout();
        }
    }else{
        $this->setLayout();
    }
//}else{
//    $this->setLayout();
//}
?>